var cheerio = require('cheerio');
var fetch = require('node-fetch');
var url = "http://detail.zol.com.cn/convenienttravel/hangzhou/#list_merchant_loc"
// var urlRoot = "http://detail.zol.com.cn"
// var url = "http://detail.zol.com.cn/cell_phone_index/subcate57_list_1.html"
var urlRoot = "http://detail.zol.com.cn"   //根地址
var urls = new Set()
var data = []
async function ad(arg){
  let url2 = arg || url;  //第一个页的地址
  var app = await fetch(url2).then(res=>res.text())  //将请求信息返回给app
  var $ = cheerio.load(app)  //用Jquery解析
  // 获取当前页所有手机的a标签
  var ele = $("#J_PicMode a.pic")
  var old_urls = []
  var urlapp = []
  //拿到所有a标签地址之后，存在数组里面，等会儿要开始爬的
  for (let i = 0; i < ele.length; i++) {
    old_urls.push(fetch(urlRoot+$(ele[i]).attr('href')).then(res=>res.text()))
  }
  urlapp = await Promise.all(old_urls)
  for (let i = 0; i < urlapp.length; i++) {
    let $2 = cheerio.load(urlapp[i],{decodeEntities: false})
    data.push({
      name:$2(".product-model__name").text(),
      price:$2(".price-type").text(),
      links: $2(".pic").attr('href')
    })
  }

  console.log(data,'shuju')


  var nextURL = $(".next").attr('href')
  if (nextURL){
    let next = await fetch(urlRoot+nextURL).then(res=>res.text())
    // ad(urlRoot+nextURL)
  }
  return data
}
ad()



